/*
 * Copyright 1999-2011 Alibaba Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.rykj.qxj.server.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class CollectionUtils {
    
    public static final int ZERO = 0;

    private static final Comparator<String> SIMPLE_NAME_COMPARATOR = new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            if (s1 == null && s2 == null) {
                return 0;
            }
            if (s1 == null) {
                return -1;
            }
            if (s2 == null) {
                return 1;
            }
            int i1 = s1.lastIndexOf('.');
            if (i1 >= 0) {
                s1 = s1.substring(i1 + 1);
            }
            int i2 = s2.lastIndexOf('.');
            if (i2 >= 0) {
                s2 = s2.substring(i2 + 1);
            }
            return s1.compareToIgnoreCase(s2);
        }
    };

    @SuppressWarnings({"unchecked", "rawtypes"})
    public static <T> List<T> sort(List<T> list) {
        if (list != null && list.size() > 0) {
            Collections.sort((List) list);
        }
        return list;
    }

    public static List<String> sortSimpleName(List<String> list) {
        if (list != null && list.size() > 0) {
            Collections.sort(list, SIMPLE_NAME_COMPARATOR);
        }
        return list;
    }

    public static Map<String, Map<String, String>> splitAll(Map<String, List<String>> list, String separator) {
        if (list == null) {
            return null;
        }
        Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>(list.size());
        for (Map.Entry<String, List<String>> entry : list.entrySet()) {
            result.put(entry.getKey(), split(entry.getValue(), separator));
        }
        return result;
    }

    public static Map<String, List<String>> joinAll(Map<String, Map<String, String>> map, String separator) {
        if (map == null) {
            return null;
        }
        Map<String, List<String>> result = new HashMap<String, List<String>>(map.size());
        for (Map.Entry<String, Map<String, String>> entry : map.entrySet()) {
            result.put(entry.getKey(), join(entry.getValue(), separator));
        }
        return result;
    }

    public static Map<String, String> split(List<String> list, String separator) {
        if (list == null) {
            return null;
        }
        Map<String, String> map = new HashMap<String, String>(list.size());
        if (list == null || list.size() == 0) {
            return map;
        }
        for (String item : list) {
            int index = item.indexOf(separator);
            if (index == -1) {
                map.put(item, "");
            } else {
                map.put(item.substring(0, index), item.substring(index + 1));
            }
        }
        return map;
    }

    public static List<String> join(Map<String, String> map, String separator) {
        if (map == null) {
            return null;
        }
        List<String> list = new ArrayList<String>();
        if (map == null || map.size() == 0) {
            return list;
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (value == null || value.length() == 0) {
                list.add(key);
            } else {
                list.add(key + separator + value);
            }
        }
        return list;
    }

    public static String join(List<String> list, String separator) {
        StringBuilder sb = new StringBuilder();
        for (String ele : list) {
            if (sb.length() > 0) {
                sb.append(separator);
            }
            sb.append(ele);
        }
        return sb.toString();
    }

    public static boolean mapEquals(Map<?, ?> map1, Map<?, ?> map2) {
        if (map1 == null && map2 == null) {
            return true;
        }
        if (map1 == null || map2 == null) {
            return false;
        }
        if (map1.size() != map2.size()) {
            return false;
        }
        for (Map.Entry<?, ?> entry : map1.entrySet()) {
            Object key = entry.getKey();
            Object value1 = entry.getValue();
            Object value2 = map2.get(key);
            if (!objectEquals(value1, value2)) {
                return false;
            }
        }
        return true;
    }

    private static boolean objectEquals(Object obj1, Object obj2) {
        if (obj1 == null && obj2 == null) {
            return true;
        }
        if (obj1 == null || obj2 == null) {
            return false;
        }
        return obj1.equals(obj2);
    }

    public static Map<String, String> toStringMap(String... pairs) {
        Map<String, String> parameters = new HashMap<String, String>();
        if (pairs.length > 0) {
            if (pairs.length % 2 != 0) {
                throw new IllegalArgumentException("pairs must be even.");
            }
            for (int i = 0; i < pairs.length; i = i + 2) {
                parameters.put(pairs[i], pairs[i + 1]);
            }
        }
        return parameters;
    }

    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> toMap(Object... pairs) {
        Map<K, V> ret = new HashMap<K, V>();
        if (pairs == null || pairs.length == 0) {
        	return ret;
        }
        if (pairs.length % 2 != 0) {
            throw new IllegalArgumentException("Map pairs can not be odd number.");
        }
        int len = pairs.length / 2;
        for (int i = 0; i < len; i++) {
            ret.put((K) pairs[2 * i], (V) pairs[2 * i + 1]);
        }
        return ret;
    }


    public static boolean isEmpty(Map<?,?> map) {
        return map == null || map.isEmpty();
    }
    
    public static boolean isNotEmpty(Map<?,?> map) {
        return !isEmpty(map);
    }
    
    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.size() == 0;
    }

    public static boolean isNotEmpty(Collection<?> collection) {
        return collection != null && collection.size() > 0;
    }
    
    public static boolean isEmpty(Object[] objects) {
    	return objects ==null?true:objects.length == 0;
    }
    
    public static boolean isNotEmpty(Object[] objects) {
    	return !isEmpty(objects);
    }
    
    
    public static boolean overCapacity(Object[] objects, int capacity) {
    	return isEmpty(objects)?false:objects.length >= capacity;
    }
    
    public static List<String> toList(Map<?, ?> map, String link) {
    	if(CollectionUtils.isEmpty(map)) {
    		return new ArrayList<String>(0);
    	}
    	
    	List<String> result = new ArrayList<String>(map.size());
    	for(Object object : map.keySet()) {
    		result.add(object + link + map.get(object));
    	}
    	return result;
    }
    
    public static void sort(List<String> values, final String link) {
    	if(CollectionUtils.isEmpty(values)) {
    		return ;
    	}
    	
		if(StringUtils.isBlank(link)) {
			Collections.sort(values);
		}
    	values.sort(new Comparator<String>() {
			@Override
			public int compare(String comp0, String comp1) {
				String[] dix1s = comp0.split(link);
				String[] dix2s = comp1.split(link);
				int dix1 = Integer.parseInt(dix1s[0]);
				int dix2 = Integer.parseInt(dix2s[0]);
				int dix12 = Integer.parseInt(dix1s[1]);
				int dix22 = Integer.parseInt(dix2s[1]);
				return dix1==dix2?(dix12>dix22?1:-1):(dix1>dix2?1:-1);
			}
		});

    }
   
    public static <T> List<T> getExtraValue(List<T> news, List<T> olds) {
    	
    	if(isEmpty(news) || isEmpty(olds)) {
    		return senseEmpty(news);
    	}
    	List<T> result = new ArrayList<T>(news.size());
    	for(T t: news) {
    		if (!olds.contains(t)) {
    			result.add(t);
    		}
    	}
    	return result;
    }
    
    public static <T> List<T> intersection(List<T> first, List<T> second) {
        List<T> inter = new ArrayList<T>(); 
        if(isEmpty(first) || isEmpty(second)) {
            return inter;            
        }
        for(T t : first) {
            if(second.contains(t)) {
                inter.add(t);
            }
        }
        return inter;
    }
       
    public static <T> List<T> getExtraValue(Set<T> news, Set<T> olds) {
        List<T> result = new ArrayList<T>(news.size());
        if(isEmpty(news)) {
            return newInstanceList();
        }else if(isEmpty(olds)){
            for(T t: news) {
                result.add(t);
            }
        }else {
            for(T t: news) {
                if (!olds.contains(t)) {
                    result.add(t);
                }
            }
        }
        return result;
    }
        
  

    public static <T>  List<T> copyLists(List<T> news) {
        if(isEmpty(news) ) {
            return senseEmpty(news);
        }
        List<T> result = newInstanceList(news.size());
        for (T t :news) {
            result.add(t);
        }
        return result;
    }
    
    /**
     * 判断如果集合为空就返回一个空集合，不为空就返回原来集合
     * @param
     * @return
     */
    public static <T> List<T> senseEmpty(List<T> collection) {
    	if(collection == null) {
    		collection = newInstanceList();
    	}
    	return collection;
    }
        
    public static <T> List<T> newInstanceList(int capacity) {
    	return new ArrayList<T>(capacity);
    }
    
    public static <T> List<T> newInstanceList() {
    	return newInstanceList(ZERO);
    }

    /**
     * 判断对象是否为空，且对象的所有属性都为空
     * ps: boolean类型会有默认值false 判断结果不会为null 会影响判断结果
     * 序列化的默认值也会影响判断结果
     *
     * @param object
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean objCheckIsNull(Object object) {
        Class clazz = (Class) object.getClass(); // 得到类对象
        Field fields[] = clazz.getDeclaredFields(); // 得到所有属性
        boolean flag = true; // 定义返回结果，默认为true
        for (Field field : fields) {
            field.setAccessible(true);
            Object fieldValue = null;
            try {
                fieldValue = field.get(object); // 得到属性值
                Type fieldType = field.getGenericType();// 得到属性类型
                String fieldName = field.getName(); // 得到属性名
                System.out.println("属性类型：" + fieldType + ",属性名：" + fieldName
                        + ",属性值：" + fieldValue);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (fieldValue != null) { // 只要有一个属性值不为null 就返回false 表示对象不为null
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static void main(String[] args) {
    	List<String> times = new ArrayList<String>();
    	times.add("10:10-10:20||0");
    	times.add("10:20-10:30||1");
    	times.add("10:00-10:10||0");
    	sort(times, "[:-]");
    	System.out.println(times);
    }
}